package GUILayer;

import java.awt.*;
import java.util.List;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import GUIadapter.CustomerModel;
import GUIadapter.PanelTableModelAdapter;
import CtrlLayer.*;
import ModelLayer.Customer;
import javax.swing.LayoutStyle.ComponentPlacement;

@SuppressWarnings("serial")
public class PanelKunde extends JPanel implements Updateable
{
	private PersonCtrl PC;
	private List<Customer> customerList;
	private PanelTableModelAdapter panelListe;
	private int indexCustomer;
			
	private PopupOpretKunde popupOpretKunde;
	private PopupRedigerKunde popupRedigerKunde;

	public PanelKunde()
	{
		setLocation(new Point(200, 100));
		setMinimumSize(new Dimension(100, 100));
		PC = new PersonCtrl();
		popupOpretKunde = new PopupOpretKunde(this);
		popupRedigerKunde = new PopupRedigerKunde(this);
		
		JButton btnRedigerKunde = new JButton("Rediger kunde");
		btnRedigerKunde.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				popupRedigerKunde.resetKunde();
				popupRedigerKunde.setVisible(true);
			}
		});

		JButton btnOpretKunde = new JButton("Opret kunde");
		btnOpretKunde.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				popupOpretKunde.clearFields();
				popupOpretKunde.setVisible(true);
			}
		});
		
		customerList = PC.getCustomerList();
		panelListe = new PanelTableModelAdapter(new CustomerModel(customerList));
		panelListe.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 2), "Kunde Kartotek", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panelListe.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		panelListe.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener()
			{
				@Override
				public void valueChanged(ListSelectionEvent event)
				{
					if (event.getValueIsAdjusting() == false)
					{
						indexCustomer = panelListe.getTable().getSelectedRow();
					}
				}
			});
				
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(panelListe, GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE))
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addComponent(btnRedigerKunde)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnOpretKunde, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panelListe, GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnOpretKunde)
						.addComponent(btnRedigerKunde))
					.addContainerGap())
		);
		setLayout(groupLayout);
	}
	
	@Override
	public void update(){
		panelListe.getTable().revalidate();
		panelListe.getTable().repaint();
	}
}
